package com.example.administrator.myapplication;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class CountryActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bt_dom1)
    public void onClick(View v) {
        AssetManager assetManager = getAssets();
        InputStream is = null;
        try {
           is = assetManager.open("country.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        switch (v.getId()){
            case R.id.bt_dom1:
                testDom(is);
                break;
        }
    }
    //Dom解析
    private void testDom(InputStream is) {

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(is);
            //获得根元素
            Element element = document.getDocumentElement();
            String country = (element.getElementsByTagName("name").item(0)).getFirstChild().getNodeValue();
            Log.i("国家名字：",country);
            NodeList provinceNodList = element.getElementsByTagName("province");
            for (int i = 0; i < provinceNodList.getLength() ; i++) {
                //得到省份节点
                Element provinceElement = (Element) provinceNodList.item(i);
                //得到省份的所有子节点
                NodeList detailNodeList = provinceElement.getChildNodes();
                for (int j = 0; j <detailNodeList.getLength() ; j++) {
                    Node detailNode = detailNodeList.item(j);
                    if (detailNode.getNodeType()==Node.ELEMENT_NODE){
                        Element detailElement = (Element) detailNode;
                        if ("name".equals(detailElement.getTagName())){
                            String province = detailElement.getFirstChild().getNodeValue();
                            Log.i("省份名字：",province);
                        }
                        else if ("citys".equals(detailElement.getTagName())){
                            NodeList cityNodeList = detailElement.getElementsByTagName("city");
                            for (int h = 0; h <cityNodeList.getLength() ; h++) {
                                Element cityElement = (Element) cityNodeList.item(h);
                                String city = cityElement.getFirstChild().getNodeValue();
                                Log.i("城市名字：",city);
                            }
                        }
                    }
                }
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
